package com.example.chenzhiran.myapplication7;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chenzhiran.myapplication7.server.clientData;
import com.example.chenzhiran.myapplication7.server.serverData;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;

public class ProductActivity extends AppCompatActivity {


    //商品相关控件
    private TextView mPriceText;
    private TextView mDescriptionText;
    private ImageView mProductImage;

    //点赞按钮
    private ImageButton mLikeButton;
    //购买按钮
    private ImageButton mPurchaseButton;


    clientData outdata3 = new clientData();
    public String productid = null;
    String username = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);

        setTitle("商品页");

        outdata3.pageId = 3;
        outdata3.dataId = 0;

        //商品图片、价格、描述
        mPriceText = (TextView) this.findViewById(R.id.product_price);
        mDescriptionText = (TextView) this.findViewById(R.id.product_description);
        mProductImage = (ImageView)this.findViewById(R.id.product_imageView);
        //点赞按钮
        mLikeButton = (ImageButton)this.findViewById(R.id.like_imageButton);
        //购买按钮
        mPurchaseButton = (ImageButton)this.findViewById(R.id.purchase_button);

        //前一页面传递信息，同时连接服务器
        Intent intent = getIntent();
        if (intent != null) {
            productid = intent.getStringExtra("productid");
            username = intent.getStringExtra("username");
            outdata3.username = username;
            outdata3.inputmessage = productid;
            new ProductActivity.connection().execute(outdata3);
        }

        mLikeButton.setOnClickListener(mOnClickListener);
        mPurchaseButton.setOnClickListener(mOnClickListener);



    }

    //点击事件
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
                //点赞按钮
                case R.id.like_imageButton:
                    outdata3.dataId = 1;
                    outdata3.username = username;
                    outdata3.inputmessage = productid;
                    new ProductActivity.connection1().execute(outdata3);
                    break;

                //购买按钮
                case R.id.purchase_button:
                    Intent intent = new Intent(ProductActivity.this, PurchaseActivity.class);
                    intent.putExtra("username",username);
                    intent.putExtra("productid",productid);

                    startActivity(intent);
                    break;
            }

        }
    };




    public class connection extends AsyncTask<clientData, Void, serverData> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected serverData doInBackground(clientData... params) {
            clientData data = params[0];
            serverData indata = new serverData();
            try {
                Socket socket = new Socket("103.242.1.105", 1080);
                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(data);
                out.flush();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                indata = (serverData) in.readObject();
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return indata;
        }

        //返回的商品信息
        @Override
        protected void onPostExecute(serverData data) {
            super.onPostExecute(data);

            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeByteArray(data.imgs.get(productid), 0,
                        data.imgs.get(productid).length);
                mProductImage.setImageBitmap(bitmap);
                mPriceText.setText(data.productdescribes.get(productid));
                mDescriptionText.setText(data.productprices.get(productid));

            } catch (Exception e) {
                e.printStackTrace();
            }

            if(data.datastatus == 0){
                mLikeButton.setImageResource(R.drawable.likepage);
            }else{
                Toast.makeText(ProductActivity.this, "点赞失败", Toast.LENGTH_SHORT).show();
            }

        }
    }


    public class connection1 extends AsyncTask<clientData, Void, serverData> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected serverData doInBackground(clientData... params) {
            clientData data = params[0];
            serverData indata = new serverData();
            try {
                Socket socket = new Socket("103.242.1.105", 1080);
                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(data);
                out.flush();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                indata = (serverData) in.readObject();
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return indata;
        }

        //返回的商品信息
        @Override
        protected void onPostExecute(serverData data) {
            super.onPostExecute(data);

            if(data.datastatus == 0){
                mLikeButton.setImageResource(R.drawable.liked);
                Toast.makeText(ProductActivity.this, "点赞成功", Toast.LENGTH_SHORT).show();
            }else{
                mLikeButton.setImageResource(R.drawable.likepage);
                Toast.makeText(ProductActivity.this, "点赞失败", Toast.LENGTH_SHORT).show();
            }

        }
    }

}
